package com.example.qt.ptplatform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.qt.ptplatform.dto.LeaderboardDto;
import com.example.qt.ptplatform.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 重置所有用户的 `is_promo` 字段（置为 0）
    @Update("UPDATE user SET is_promo = 0")
    void resetUserPromoStatus();

    // 根据用户等级更新符合条件的用户的 `is_promo` 字段
    @Update("UPDATE user SET is_promo = 1 WHERE level >= #{criteria}")
    void updatePromoStatusForEligibleUsers(int criteria);

    //更新用户的总积分
    @Update("UPDATE user SET user_points = user_points + #{points} WHERE user_id = #{userId}")
    int updateUserPoints(@Param("userId") Integer userId,
                         @Param("points") BigDecimal points);

    @Select("SELECT user_points FROM user WHERE user_id = #{userId}")
    Integer getUserPointsById(@Param("userId") Integer userId);

    //获取所有用户
    @Select("SELECT * FROM user")
    List<User> getAllUsers();

    // 查询用户的分享率
    @Select("SELECT share_rate FROM user WHERE user_id = #{userId}")
    Double getShareRateByUserId(@Param("userId") Integer userId);

    // 更新用户分享率
    @Update("UPDATE user SET share_rate = #{shareRate} WHERE user_id = #{userId}")
    void updateShareRate(@Param("userId") Integer userId, @Param("shareRate") Double shareRate);

    // 更新用户等级
    @Update("UPDATE user SET level = #{level} WHERE user_id = #{userId}")
    void updateUserLevel(@Param("userId") Integer userId, @Param("level") Integer level);

    // 获取用户的上传量
    @Select("SELECT upload_count FROM user WHERE user_id = #{userId}")
    Double getUploadAmountByUserId(Integer userId);

    // 获取用户的下载量
    @Select("SELECT download_count FROM user WHERE user_id = #{userId}")
    Double getDownloadAmountByUserId(Integer userId);

    // 更新用户是否允许匿名评论
    @Update("UPDATE user SET allow_anonymous_comment = #{allowAnonymousComment} WHERE user_id = #{userId}")
    void updateAnonymousCommentPermission(@Param("userId") Integer userId, @Param("allowAnonymousComment") Boolean allowAnonymousComment);

    // 更新用户每月的邀请码数量
    @Update("UPDATE user SET invite_count = #{inviteCount} WHERE user_id = #{userId}")
    void updateInviteCount(@Param("userId") Integer userId, @Param("inviteCount") int inviteCount);

    // 更新用户资料，只更新允许修改的字段
    @Update("UPDATE user SET nickname = #{nickname},sex = #{sex}, introduction = #{introduction} " +
            "WHERE user_id = #{userId}")
    int updateUser(User user);

    // 查询用户昵称和积分，并按积分降序排序
    @Select("SELECT nickname, user_points AS userPoints FROM user ORDER BY user_points DESC")
    List<LeaderboardDto> selectLeaderboard();

    // 获取所有拥有增量卡的用户
    List<User> selectUsersWithIncrementalCard();
}

